package com.dizner.dimagepicker;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.TreeMap;

public class Utils {

    private static Context applicationContext;

    private static TreeMap<String,String> dirKeyMap;

    public static void init(Context context){
        applicationContext = context;
    }


    static Uri getImageContentUri(Context context, String path) {
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
                new String[]{path}, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            // 如果图片不在手机的共享图片数据库，就先把它插入。
            if (new File(path).exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, path);
                return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

    private static void loadDifKeyMap(){

        InputStream inputStream = null;
        try {
            inputStream = applicationContext.getResources().getAssets().open("file_dir_key.json");
            byte[] byteArray = new byte[1024];
            StringBuilder builder = new StringBuilder();
            int len;
            while (-1!=(len = inputStream.read(byteArray))){
                builder.append(new String(byteArray,0,len));
            }

            String json = builder.toString();
            dirKeyMap = new TreeMap<>();
            try {
                JSONArray jsonArray = new JSONArray(json);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String targetPath = jsonObject.optString("path");
                    String extName = jsonObject.optString("name");

                    dirKeyMap.put(targetPath,extName);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    static String getFileDirExtName(String path){
        if (dirKeyMap == null) {
            loadDifKeyMap();
        }
        for (String key : dirKeyMap.keySet()) {
            String extName = dirKeyMap.get(key);
            if (TextUtils.equals(path, key)) {
                return extName;
            }
        }
        return "";
    }
}
